<?php


namespace app\admin\controller;


use app\admin\common\mail;
use app\BaseController;
use think\facade\View;
use think\Request;

class Email extends BaseController
{
    protected $mail_total = null;
    protected $mail = null;

    public function initialize()
    {
        parent::initialize();
        $username = '805858483@qq.com';
        $password = 'yfjgfgdbmurvbbdb';
        $email_address = 'imap.qq.com';
        $mail_server = 'imap.qq.com';
        $server_type = 'imap';
        $port = 143;
        $this->mail = new mail($username, $password, $email_address, $mail_server, $server_type, $port);
        $this->mail->connect();
        $this->mail_total = $this->mail->get_mail_total();
    }

    //资源显示
    public function index(Request $request)
    {

        if ($request->isAjax()) {

            $data = [];
            for ($i = $this->mail_total; $i > 0; $i--) {
                if ($this->mail->get_imap_header($i) != false) {
                    $data[] = $this->mail->get_header_info($this->mail->get_imap_header($i), $i);
                }
            }

            return json(['code' => 200, 'msg' => '', 'total' => $this->mail_total, 'data' => $data]);
        }

        return View::fetch();
    }


    public function getBody(Request $request)
    {
       if ($request->isAjax()){
           $mid = $request->param('id');
           $get_body = $this->mail->get_body($mid);
           return json(['code' => 200, 'url' => '', 'msg' => '','data'=>$get_body]);
       }
    }

    //发送邮件
    public function add()
    {
        return View::fetch();
    }
}